package br.org.acessobrasil.correiobraille.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

	public class MDraw {
		private Graphics2D graphics;
		private int lineHeight;
		private boolean textAlignRight = false;

		public MDraw(Graphics2D graphics) {
			this.graphics = graphics;
			graphics.setColor(Color.BLACK);
			Font f1= new Font("Arial",Font.BOLD,11);
			graphics.setFont(f1);
		}
		
		public void alignRight(){
			textAlignRight = true;
		}
		public void alignLeft(){
			textAlignRight = false;
		}
		
		public void drawString(String texto, int x, int y) {
			
			//Font font = Font.getFont("Helvetica-bold-italic");
			Font font= new Font("Arial",Font.LAYOUT_RIGHT_TO_LEFT ,11);
			FontRenderContext frc = graphics.getFontRenderContext();

			//Rectangle2D bounds = layout.getBounds();
			//bounds.setRect(bounds.getX() + x, bounds.getY() + y, bounds.getWidth()+10, bounds.getHeight()+10);
			//graphics.draw(bounds);
			
			String linha[] = texto.split("\n");
			for (int i = 0; i < linha.length; i++) {
				if(textAlignRight){
					BufferedImage a = new BufferedImage(1024,200,BufferedImage.TYPE_INT_RGB);
					TextLayout layout = new TextLayout(linha[i], font, frc);
					layout.draw(a.createGraphics(),0f, 0f);
					Rectangle2D bounds = layout.getBounds();
					graphics.drawString(linha[i], x - (int)bounds.getWidth(), y);
				}else{
					graphics.drawString(linha[i], x, y);
				}
				y += lineHeight;
			}
			/*
			String linha[] = texto.split("\n");
			for (int i = 0; i < linha.length; i++) {
				graphics.drawString(linha[i], x, y);
				y+=lineHeight;
			}*/
		}

		public void dispose() {
			graphics.dispose();
		}

		public void setLineHeight(int lineHeight) {
			this.lineHeight = lineHeight;
		}

		public int getLineHeight() {
			return lineHeight;
		}
	}